<?php
/**
 * Created by PhpStorm.
 * User: Peco
 * Date: 2019/9/27
 * Time: 22:51
 */

namespace App\Services;

use App\Traits\Setting;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

/**
 * Class ProcessService
 * @package App\Services
 */
trait ProcessService
{
    use Setting;

    /**
     * 物流查询
     * @param $shipNo
     * @return mixed
     */
    public function shipsQuery($shipNo){
        try{
            if($config = $this->getSettingCache(ALI_KEY)){
                $config = \GuzzleHttp\json_decode($config);
                $host = data_get($config, 'ship_host');
                $appCode = data_get($config, 'app_code');
                $headers = array();
                array_push($headers, "Authorization:APPCODE " . $appCode);

                //请求地址 系统自动识别-AUTO 否则输入快递物流公司编码
                $url = $host . '?NO=' .$shipNo."&TYPE=AUTO";
                $curl = curl_init();
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
                curl_setopt($curl, CURLOPT_URL, $url);
                curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
                curl_setopt($curl, CURLOPT_FAILONERROR, false);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_HEADER, false);
                if (1 == strpos("$".$host, "https://")) {
                    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
                }

                return \GuzzleHttp\json_decode(curl_exec($curl), true);
            }

            return false;

        }catch (\Exception $e){
            Log::error('物流查询错误：'.$e->getMessage());
            return false;
        }
    }

    /**
     * 用户实名认证
     * @param array $data
     * @return mixed
     */
    public function realNameAuthentication(array $data){
        try{
            $config = Redis::get(ALI_KEY);
            $host = data_get($config, 'real_host');
            $appCode = data_get($config, 'app_code');
            $headers = array();
            array_push($headers, "Authorization:APPCODE " . $appCode);

            //请求地址
            $url = $host . '?idcard=' .$data['idCard'].'&name='.$data['userName'];

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_FAILONERROR, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HEADER, false);
            if (1 == strpos("$".$host, "https://")) {
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            }

            return \GuzzleHttp\json_decode(curl_exec($curl), true);
        }catch (\Exception $e){
            Log::error('用户实名认证错误：'.$e->getMessage());
            return false;
        }
    }

    /**
     * IP地址查询
     * @return mixed
     */
    public function userIPAddressQuery(){
        try{
            $config = Redis::get(ALI_KEY);
            $host = data_get($config, 'ip_host');
            $appCode = data_get($config, 'app_code');
            $headers = array();
            array_push($headers, "Authorization:APPCODE " . $appCode);

            //获取ip
            $ip = getClientIp();

            //请求地址
            $url = $host . "?ip=".$ip;

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_FAILONERROR, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HEADER, false);
            if (1 == strpos("$".$host, "https://")) {
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            }

            return \GuzzleHttp\json_decode(curl_exec($curl), true);

        }catch (\Exception $e){
            Log::error('IP地址查询错误：'.$e->getMessage());
            return false;
        }
    }
}
